﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace uz.control
{
    public class UzTreeNodeCollection:System.Collections.IEnumerable
    {
        #region 父节点：Parent
        /// <summary>
        /// 父节点
        /// </summary>
        private UzTreeNode _Parent = null;
        /// <summary>
        /// 父节点
        /// </summary>
        internal UzTreeNode Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }
        #endregion

        #region 节点的容器控件：UzTreeView
        /// <summary>
        /// 节点的容器控件
        /// </summary>
        private UzTreeView _UzTreeView = null;
        /// <summary>
        /// 节点的容器控件
        /// </summary>
        internal UzTreeView UzTreeView
        {
            get 
            {
                if (Parent == null)
                {
                    return _UzTreeView;
                }
                else
                {
                    return Parent.UzTreeView;
                }
            }
            set { 
                _UzTreeView = value; 
            }
        }
        #endregion

        /// <summary>
        /// 受到保护的构造函数
        /// </summary>
        internal UzTreeNodeCollection()
        {
        }

        internal UzTreeNodeCollection(UzTreeView view)
        {
            this.UzTreeView = view;
        }

        private List<UzTreeNode> _Items = new List<UzTreeNode>();

        /// <summary>
        /// 基于数字的索引器
        /// </summary>
        /// <param name="Index">索引</param>
        /// <returns>节点</returns>
        public UzTreeNode this[int Index]
        {
            get
            {
                return _Items[Index];
            }
            set
            {
                _Items[Index] = value;
            }
        }

        /// <summary>
        /// 返回子节点的个数
        /// </summary>
        public int Count
        {
            get
            {
                return _Items.Count;
            }
        }
        

        /// <summary>
        /// 添加一个子节点
        /// </summary>
        /// <param name="node">要添加的节点</param>
        public void Add(UzTreeNode node)
        {
            node.Parent = this.Parent;
            node.UzTreeView = UzTreeView;
            node.Index = _Items.Count;
            _Items.Add(node);
        }

        /// <summary>
        /// 插入一个子节点
        /// </summary>
        /// <param name="node">要插入的子节点</param>
        public void Insert(int index, UzTreeNode node)
        {
            node.Parent = this.Parent;
            node.UzTreeView = UzTreeView;
            node.Index = index;
            _Items.Insert(index, node);

            for (int i = index + 1; i < _Items.Count; i++)
            {
                _Items[i].Index = i;
            }
        }

        public void RemoveAt(int index)
        {
            _Items.RemoveAt(index);

            for (int i = index; i < _Items.Count; i++)
            {
                _Items[i].Index = i;
            }
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            return _Items.GetEnumerator();
        }
        
    }
}
